package xyz.xtt.third.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.common.starter.cache.CacheManager;
import xyz.xtt.third.constants.ThirdCache;
import xyz.xtt.third.entity.TblThirdOrder;
import xyz.xtt.third.mapper.TblThirdOrderMapper;
import xyz.xtt.third.service.ITblThirdOrderService;

/**
 * <p>
 * 玫瑰花三方订单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-11
 */
@Service
public class TblThirdOrderServiceImpl extends ServiceImpl<TblThirdOrderMapper, TblThirdOrder> implements ITblThirdOrderService {
	@Autowired
	private CacheManager cacheManager;

	@Override
	public TblThirdOrder infoCahceWithOrderNumer(String orderNumber) {
		return cacheManager.cacheObj(() -> infoWithOrderNumer(orderNumber), ThirdCache.TBL_THIRD_ORDER, orderNumber);
	}

	@Override
	public TblThirdOrder infoWithOrderNumer(String orderNumber) {
		LambdaQueryChainWrapper<TblThirdOrder> query = new LambdaQueryChainWrapper<>(this.baseMapper);
		return query.eq(TblThirdOrder::getOrderNumber, orderNumber).one();
	}

	@Override
	public Boolean updateResult(String orderNumber, Long uid, Integer userType, Long tid) {
		LambdaUpdateChainWrapper<TblThirdOrder> update = new LambdaUpdateChainWrapper<>(baseMapper);
		return update
				.eq(TblThirdOrder::getOrderNumber, orderNumber)
				.eq(TblThirdOrder::getResult, 0)
				.set(TblThirdOrder::getUserId, uid)
				.set(TblThirdOrder::getUserType, userType)
				.set(TblThirdOrder::getResult, 1)
				.set(TblThirdOrder::getNextTime, LocalDateTime.now())
				.set(TblThirdOrder::getTid, tid)
				.update();
	}

}
